/*
package com.zjy.AIAnswer.manager;

import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionRequest;
import com.volcengine.ark.runtime.model.completion.chat.ChatMessage;
import com.volcengine.ark.runtime.model.completion.chat.ChatMessageRole;
import com.volcengine.ark.runtime.service.ArkService;
import com.zjy.AIAnswer.common.ErrorCode;
import com.zjy.AIAnswer.exception.BusinessException;
import io.reactivex.Flowable;
import okhttp3.ConnectionPool;
import okhttp3.Dispatcher;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class DeepSeekManager {

    // 配置参数
*/
/*
    @Value("${deepseek.api-key}")
    private String apiKey;

    @Value("${deepseek.base-url}")
    private String baseUrl;

    @Value("${deepseek.model}")
    private String model;
*//*


    // 连接配置
    private static final int MAX_IDLE_CONNECTIONS = 5;
    private static final long KEEP_ALIVE_DURATION = 30L;
    private static final int TIMEOUT_SECONDS = 1800;

    // 温度参数
    private static final double STABLE_TEMPERATURE = 0.05f;
    private static final double UNSTABLE_TEMPERATURE = 0.99f;

    private ArkService arkService;

    @PostConstruct
    private void initService() {
        ConnectionPool pool = new ConnectionPool(MAX_IDLE_CONNECTIONS, KEEP_ALIVE_DURATION, TimeUnit.SECONDS);
        Dispatcher dispatcher = new Dispatcher();

        this.arkService = ArkService.builder()
                .timeout(Duration.ofSeconds(TIMEOUT_SECONDS))
                .connectTimeout(Duration.ofSeconds(20))
                .dispatcher(dispatcher)
                .connectionPool(pool)
                .baseUrl(baseUrl)
                .apiKey(apiKey)
                .build();
    }

    */
/**
     * 同步请求（答案不稳定）
     *//*

    public String doSyncUnstableRequest(String systemMessage, String userMessage) {
        return doSyncRequest(systemMessage, userMessage, UNSTABLE_TEMPERATURE);
    }

    */
/**
     * 同步请求（答案稳定）
     *//*

    public String doSyncStableRequest(String systemMessage, String userMessage) {
        return doSyncRequest(systemMessage, userMessage, STABLE_TEMPERATURE);
    }

    */
/**
     * 自定义温度同步请求
     *//*

    public String doSyncRequest(String systemMessage, String userMessage, Double temperature) {
        List<ChatMessage> messages = buildMessages(systemMessage, userMessage);
        return doRequest(messages, temperature, false);
    }

    */
/**
     * 通用流式请求
     *//*

    public Flowable<String> doStreamRequest(String systemMessage, String userMessage, Double temperature) {
        List<ChatMessage> messages = buildMessages(systemMessage, userMessage);
        return doStreamRequest(messages, temperature);
    }

    */
/**
     * 构建消息列表
     *//*

    private List<ChatMessage> buildMessages(String systemMessage, String userMessage) {
        List<ChatMessage> messages = new ArrayList<>();
        ChatMessage systemChatMessage =  ChatMessage.builder().role(ChatMessageRole.SYSTEM).content(systemMessage).build();
        ChatMessage userChatMessage = ChatMessage.builder().role(ChatMessageRole.USER).content(userMessage).build();
        messages.add(systemChatMessage);
        messages.add(userChatMessage);
        return messages;
    }

    */
/**
     * 核心请求方法
     *//*

    */
/**
     * 修复后的核心请求方法
     *//*

    private String doRequest(List<ChatMessage> messages, Double temperature, Boolean stream) {
        try {
            ChatCompletionRequest request = buildRequest(messages, temperature);

            return arkService.createChatCompletion(request)
                    .getChoices()
                    .stream()
                    .findFirst()
                    .map(choice -> {
                        Object reasoningContent = choice.getMessage().getReasoningContent();
                        Object content = choice.getMessage().getContent();

                        // 类型安全转换
                        return StringUtils.firstNonBlank(
                                safeToString(reasoningContent),
                                safeToString(content)
                        );
                    })
                    .orElseThrow(() -> new BusinessException(ErrorCode.SYSTEM_ERROR, "Empty response"));
        } catch (Exception e) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "API调用失败: " + e.getMessage());
        }
    }

    */
/**
     * 修复后的核心流式请求方法
     *//*

    private Flowable<String> doStreamRequest(List<ChatMessage> messages, Double temperature) {
        try {
            ChatCompletionRequest request = buildRequest(messages, temperature);

            return arkService.streamChatCompletion(request)
                    .doOnError(Throwable::printStackTrace)
                    .map(delta -> {
                        if (delta.getChoices().isEmpty()) {
                            return "";
                        }
                        Object reasoningContent = delta.getChoices().get(0).getMessage().getReasoningContent();
                        Object content = delta.getChoices().get(0).getMessage().getContent();

                        // 类型安全转换
                        return StringUtils.firstNonBlank(
                                safeToString(reasoningContent),
                                safeToString(content)
                        );
                    });
        } catch (Exception e) {
            return Flowable.error(new BusinessException(ErrorCode.SYSTEM_ERROR, "流式请求失败: " + e.getMessage()));
        }
    }

    */
/**
     * 安全类型转换方法
     *//*

    private String safeToString(Object obj) {
        if (obj == null) {
            return null;
        }
        if (obj instanceof CharSequence) {
            return obj.toString();
        }
        throw new BusinessException(ErrorCode.SYSTEM_ERROR, "Unexpected content type: " + obj.getClass());
    }

    */
/**
     * 统一构建请求
     *//*

    private ChatCompletionRequest buildRequest(List<ChatMessage> messages, Double temperature) {
        return ChatCompletionRequest.builder()
                .model(model)
                .messages(messages)
                .temperature(temperature)
                .build();
    }
}
*/
